package com.lc.auth.controller;

import com.lc.auth.service.SysDeptService;
import com.lc.common.result.Result;
import com.lc.model.system.SysDept;
import com.lc.system.enums.BusinessType;
import com.lc.vo.system.SysDeptQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.lc.system.annotation.Log;


import java.util.List;


/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author yqr
 * @since 2023-06-10
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/admin/system/sysDept")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    //展示树型数据列表
    @Log(title = "部门管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    @ApiOperation(value = "列表数据展示")
    @GetMapping("list")
    public Result getList(){
        List<SysDept> sysDeptList = sysDeptService.getDeptTreeList();
        return Result.ok(sysDeptList);
    }

    //新增
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysDept.add')")
    @ApiOperation(value = "新增部门接口")
    @PostMapping("add")
    public Result addDept(@RequestBody SysDept sysDept){
        sysDeptService.saveDept(sysDept);
        return Result.ok().message("部门新增成功");
    }

    //删除
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysDept.remove')")
    @ApiOperation(value = "删除部门接口")
    @DeleteMapping("delete/{id}")
    public Result deleteDept(@PathVariable String id){
        sysDeptService.removeById(id);
        return Result.ok().message("部门删除成功!");
    }


    //编辑
    @Log(title = "部门管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    @ApiOperation(value = "编辑部门接口")
    @GetMapping("edit/{id}")
    public Result editDept(@PathVariable String id){
        SysDept sysDept = sysDeptService.getById(id);
        String parentName = sysDeptService.getParentName(sysDept);
        SysDeptQueryVo sysDeptQueryVo = new SysDeptQueryVo();
        BeanUtils.copyProperties(sysDept, sysDeptQueryVo);//对象拷贝
        sysDeptQueryVo.setParentName(parentName);
        return Result.ok(sysDeptQueryVo);
    }

    //修改
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    @ApiOperation(value = "修改部门接口")
    @PutMapping("update")
    public Result update(@RequestBody SysDept sysDept){
        sysDeptService.updateById(sysDept);
        return Result.ok().message("部门修改成功");
    }

}

